package dbinit.csv;

import java.io.FileNotFoundException;
import java.io.IOException;

import core.address.model.continent.Continent;
import core.address.model.country.Country;
import core.address.model.region.Region;
import core.address.model.state.State;

import dbinit.InitCity;
import dbinit.InitContinent;
import dbinit.InitCountry;
import dbinit.InitRegion;
import dbinit.InitState;
import dbinit.csv.csvreader.CsvReader;

public class ImportGEO 
{
	private static void importCity() 
	{
		try 
		{
				System.out.println("start import city");
				CsvReader data = new CsvReader("source/GEO/city.csv");
				data.readHeaders();
				int count = 0 ;
				while (data.readRecord())
				{
					String city=data.get("city");
					String region=data.get("region");
					String state=data.get("state");
					String country=data.get("country");
					String continent=data.get("continent");
					if(city != null && city.trim().length() != 0)
					{
						Continent conti = InitContinent.getContinentByUuid(InitContinent.doinit(continent.trim()));
						Country countr = InitCountry.getCountryByName(country.trim());
						if(countr == null)
						{
							 countr = InitCountry.getCountryByUuid(InitCountry.doinit(country.trim(),conti,"")) ;
						}
						State s = InitState.getStateByName(state.trim());
						if(s == null)
						{
							 s = InitState.getStateByUuid(InitState.doinit(countr, state.trim(), conti, ""));
						}
						Region r = InitRegion.getRegionByUuid(InitRegion.doinit(region.trim(), s, countr, conti));
						InitCity.doinit(s, r, countr, conti, city.trim());
						count++ ;
					}
				}
				System.out.println("import finished... " + count + " rows imported");
				data.close();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		} 
	}
	
	public static void importRegion()
	{
		try 
		{
				System.out.println("start import region");
				CsvReader data = new CsvReader("source/GEO/region.csv");
				data.readHeaders();
				int count = 0 ;
				while (data.readRecord())
				{
					String region=data.get("region");
					String state=data.get("state");
					String country=data.get("country");
					String continent=data.get("continent");
					if(region != null && region.trim() != "")
					{
						Continent conti = InitContinent.getContinentByUuid(InitContinent.doinit(continent.trim()));
						Country countr = InitCountry.getCountryByName(country.trim());
						if(countr == null)
						{
							 countr = InitCountry.getCountryByUuid(InitCountry.doinit(country.trim(),conti,"")) ;
						}
						State s = InitState.getStateByName(state.trim());
						if(s == null)
						{
							 s = InitState.getStateByUuid(InitState.doinit(countr, state.trim(), conti, ""));
						}
						InitRegion.doinit(region, s, countr, conti);
						count++ ;
					}
					
				}
				System.out.println("import finished... " + count + " rows imported");
				data.close();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		} 
	}
	public static void importStates()
	{
		try 
		{
				System.out.println("start import states");
				CsvReader data = new CsvReader("source/GEO/states.csv");
				data.readHeaders();
				int count = 0 ;
				while (data.readRecord())
				{
					String state=data.get("state");
					String country=data.get("country");
					String continent=data.get("continent");
					String abbreviation=data.get("abbreviation");
					if(state != null && state.trim() != "")
					{
						Continent conti = InitContinent.getContinentByUuid(InitContinent.doinit(continent.trim()));
						Country countr = InitCountry.getCountryByName(country.trim());
						if(countr == null)
						{
							 countr = InitCountry.getCountryByUuid(InitCountry.doinit(country.trim(),conti,"")) ;
						}
						InitState.doinit(countr, state, conti ,abbreviation);
						count++ ;
					}
				}
				System.out.println("import finished... " + count + " rows imported");
				data.close();
		} 
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		} 
	}
	public static void importCountries()
	{
		try 
		{
				System.out.println("start import country");
				CsvReader data = new CsvReader("source/GEO/country.csv");
				data.readHeaders();
				int count = 0 ;
				while (data.readRecord())
				{
					String country=data.get("country");
					String continent=data.get("continent");
					String abbreviation=data.get("abbreviation");
					if(country != null && country.trim()!= "")
					{
						Continent conti = InitContinent.getContinentByUuid(InitContinent.doinit(continent));
						InitCountry.doinit(country.trim(), conti ,abbreviation);
						count++ ;
					}
				}
				System.out.println("import finished... " + count + " rows imported");
				data.close();
		} 
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		} 
	}
	public static void importContinents()
	{
		try 
		{
				CsvReader data = new CsvReader("source/GEO/continents.csv");
				data.readHeaders();
				while (data.readRecord())
				{
					String name=data.get("name");
					if(name != null && name.trim().length() != 0)
					{
						name = name.trim();
						InitContinent.doinit(name);
					}
				}
				data.close();
		} 
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		} 
	}
	public static void doImport()
	{
		///importContinents();
		//importCountries();
		//importStates();
		//importRegion() ;
		importCity();
	}
	public static void main(String argv[])
	{
		doImport();
	}
}
	
